﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace LevelGenerator
{
    public class Corridor
    {
        public Color color = Color.Gray;
        private Point p, q, pq;
        private Rectangle rect1, rect2;
        private int CorridorType;
        private int thickness = 2;
        private int CorridorWidth = 40;
        private int wallBorder = 2;


        public Rectangle Rect1
        {
            get { return rect1; }
        }

        public Rectangle Rect2
        {
            get { return rect2; }
        }

        public Corridor(Point p1, Point p2, int type = 0)
        {
            if (type == 0)
            {
                Random r = new Random();
                CorridorType = r.Next(0, 1);
            }
            else
                CorridorType = type;


            if (p1.X < p2.X)
            {
                p = p1;
                q = p2;
            }
            else
            {
                p = p2;
                q = p1;
            }


            if (p.Y >= q.Y)
            {
                if (CorridorType == 0)
                {
                    //working
                    pq = new Point(p.X - CorridorWidth / 2, q.Y - CorridorWidth / 2);
                    rect1 = new Rectangle(pq.X, pq.Y, q.X - pq.X, CorridorWidth);
                    rect2 = new Rectangle(pq.X, pq.Y, CorridorWidth, p.Y - pq.Y);
                }
                else
                {
                    //IT FUCKING WORKs
                    pq = new Point(q.X + CorridorWidth / 2, p.Y - CorridorWidth / 2);
                    rect1 = new Rectangle(q.X - CorridorWidth / 2, q.Y, CorridorWidth, pq.Y - q.Y);
                    rect2 = new Rectangle(p.X, p.Y - CorridorWidth / 2, pq.X - p.X, CorridorWidth);
                }
            }
            else
            {
                if (CorridorType == 0)
                {
                    //working
                    pq = new Point(q.X + CorridorWidth / 2, p.Y - CorridorWidth / 2);
                    rect1 = new Rectangle(p.X, pq.Y, pq.X - p.X, CorridorWidth);
                    rect2 = new Rectangle(q.X - CorridorWidth / 2, p.Y - CorridorWidth / 2, CorridorWidth, q.Y - pq.Y);
                }
                else
                {
                    //working
                    pq = new Point(p.X - CorridorWidth / 2, q.Y + CorridorWidth / 2);
                    rect1 = new Rectangle(pq.X, p.Y, CorridorWidth, pq.Y - p.Y);
                    rect2 = new Rectangle(pq.X, pq.Y - CorridorWidth, q.X - pq.X, CorridorWidth);

                }
            }


        }
        public void Draw(SpriteBatch spriteBatch, GraphicsDevice gDevice)
        {
            spriteBatch.Begin();
            DrawCorridorFloor(gDevice, spriteBatch);
            spriteBatch.End();
        }
        public void DrawCorridorFloor(GraphicsDevice gDevice, SpriteBatch spriteBatch)
        {
            DrawCorridor(rect1, rect2, gDevice, spriteBatch, color);
        }
        public void DrawCorridor(Rectangle r1, Rectangle r2, GraphicsDevice gDevice, SpriteBatch spriteBatch, Color color)
        {
            var rect = new Texture2D(gDevice, 1, 1);
            rect.SetData(new[] { color });
            spriteBatch.Draw(rect, r1, color);
            spriteBatch.Draw(rect, r2, color);
        }
    }
}
